package com.ruijie.acl.web;

import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclDeviceTimeRange;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.service.DeviceTimeRangeService;
import com.ruijie.acl.service.TimeRangeService;
import com.ruijie.emp.device.Device;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;

/**
 * <p>
 * Title: ACL时间段与ACL设备关联管理Action类

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("deviceTimeRangeAction")
@Scope(ScopeType.CONVERSATION)
public class DeviceTimeRangeAction extends AclPlanBaseAction {

	@In("#{deviceTimeRangeService}")
	private DeviceTimeRangeService deviceTimeRangeService;
	
	private ListDataModel timeRangeEntityModelList = null;
	
	private ListDataModel aclDeviceEntityModelList = null;
	
	@In("#{timeRangeService}")
	private TimeRangeService timeRangeService;
		
	//ACL设备信息
	private AclDevice aclDevice;
	private Long deviceId = null;
	//ACL时间段信息
	private TimeRange timeRange;
	private Long timeRangeId = null;
	
	private String differenceObject = new String();
	
	/**
	 * 取得ACL组分页信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getTimeRangeEntityModelList(AclDevice aclDevice) {
		differenceObject = AclDevice.class.getName();
		if(aclDevice != null) {
			this.aclDevice = aclDevice;
			//if(timeRangeEntityModelList == null || aclDevice.getId() != this.deviceId) {
				currentPage = deviceTimeRangeService.pagedQueryByDevcie(
						aclDevice, page, pageSize);
				timeRangeEntityModelList = new ListDataModel(
						(List<AclDeviceTimeRange>) currentPage.getResult());
			//}
			this.deviceId = aclDevice.getId();
		}
		
		return timeRangeEntityModelList;
	}
	
	/**
	 * 取得ACL组分页信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAclDeviceEntityModelList(TimeRange timeRange) {
		differenceObject = TimeRange.class.getName();
		if(timeRange != null) {
			this.timeRange = timeRange;
			//if(aclDeviceEntityModelList == null || timeRange.getId() != this.timeRangeId) {
				currentPage = deviceTimeRangeService.pagedQueryByTimeRange(timeRange, page, pageSize);
				aclDeviceEntityModelList = new ListDataModel((List<AclDeviceTimeRange>) currentPage.getResult());
			//}
			this.timeRangeId = timeRange.getId();
		}
		
		return aclDeviceEntityModelList;
	}
	
	
	public void delete(AclDeviceTimeRange entity) {
		deviceTimeRangeService.remove(entity);
		TimeRange timeRangeDevice = timeRangeService.getByNameWithAclDevice(entity.getTimeRange().getName(), entity.getAclDevice());
		if (timeRangeDevice != null){
			timeRangeService.remove(timeRangeDevice);
		}
		updateModel();
	}
	
	@Override
	protected void updateModel() {
		if(AclDevice.class.getName().equals(differenceObject)) {
			this.timeRangeEntityModelList = null;
			getTimeRangeEntityModelList(aclDevice);
		}else if(TimeRange.class.getName().equals(differenceObject)) {
			this.aclDeviceEntityModelList = null;
			getAclDeviceEntityModelList(timeRange);
		}else{
			this.timeRangeEntityModelList = null;
			this.aclDeviceEntityModelList = null;
			getAclDeviceEntityModelList(timeRange);
			getTimeRangeEntityModelList(aclDevice);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return deviceTimeRangeService;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return null;
	}

	@Override
	protected String getListPage() {
		return null;
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {
	}

	@Override
	public void setCriteria(EntityObject criteria) {
		
	}

	@Override
	public void setModel(EntityObject model) {
	
	}

	public ListDataModel getTimeRangeEntityModelList() {
		return timeRangeEntityModelList;
	}

	public void setTimeRangeEntityModelList(ListDataModel timeRangeEntityModelList) {
		this.timeRangeEntityModelList = timeRangeEntityModelList;
	}

	public ListDataModel getAclDeviceEntityModelList() {
		return aclDeviceEntityModelList;
	}

	public void setAclDeviceEntityModelList(ListDataModel aclDeviceEntityModelList) {
		this.aclDeviceEntityModelList = aclDeviceEntityModelList;
	}

	public AclDevice getAclDevice() {
		return aclDevice;
	}

	public void setAclDevice(AclDevice aclDevice) {
		this.aclDevice = aclDevice;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}

	public TimeRange getTimeRange() {
		return timeRange;
	}

	public void setTimeRange(TimeRange timeRange) {
		this.timeRange = timeRange;
	}

	public Long getTimeRangeId() {
		return timeRangeId;
	}

	public void setTimeRangeId(Long timeRangeId) {
		this.timeRangeId = timeRangeId;
	}
	
	/**
	 * 时间段详细界面的单设备部署
	 * @param id
	 * @param device
	 */
	public void reConfig(String id,Device device){
		TimeRange timeRange = timeRangeService.get(Long.valueOf(id));
		try {
			timeRangeService.reConfig(timeRange, device,"modify");
		} catch (BusinessException e) {
			throw e;
		}
		updateModel();
	}
	
	/**
	 * 删除ACL时间段的部署

	 * @param id
	 * @param device
	 */
	public void deleteDeviceConfig(String id,Device device,AclDeviceTimeRange entity){		

		TimeRange timeRange = timeRangeService.get(Long.valueOf(id));
		try {
			if(!timeRangeService.isExitesAce(timeRange)){
				timeRangeService.reConfig(timeRange, device,"DeleteTimeRange");
			}else{
				throw new BusinessException("acl_time_delete_use_error", timeRange.getName());
			}
		} catch (BusinessException e) {
			throw e;
		}
		this.delete(entity);
		updateModel();
	}

}
